package com.etc.controller;

import com.etc.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("user")
public class UserController {

    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String login(User user, HttpServletRequest request) {

        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
        try {
            //为当前用户进行认证，授权
            subject.login(token);
            request.setAttribute("user", user);
            return "success";

        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("user", user);
            request.setAttribute("errorMsg", "用户名或密码错误！");
            return "login";
        }
    }

    @RequestMapping("/teacher")
    public String index() {
        return "index";
    }

}
